<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx 与后端应用服务器通信指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        
        .code-block code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.05);
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            font-weight: 600;
        }
        
        .list-item {
            position: relative;
            padding-left: 28px;
        }
        
        .list-item::before {
            content: '▸';
            position: absolute;
            left: 0;
            color: #667eea;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Nginx 与后端应用服务器
                    <span class="block text-3xl md:text-4xl mt-4 opacity-90">通信架构指南</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    深入理解反向代理、负载均衡与高性能 Web 架构
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-server text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">反向代理</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-balance-scale text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">负载均衡</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">安全通信</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="max-w-4xl mx-auto mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700 indent-8">
                    Nginx 与后端应用服务器（如 Tomcat、Node.js）的通信是 Web 服务器和应用服务器之间的关键环节。Nginx 作为反向代理服务器，将客户端的请求转发给后端应用服务器处理，获取结果后再将响应返回给客户端。
                </p>
            </div>
        </div>

        <!-- Architecture Diagram -->
        <div class="max-w-6xl mx-auto mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">系统架构概览</h2>
                <div class="mermaid">
                    graph LR
                        A[客户端] -->|HTTP/HTTPS| B[Nginx]
                        B -->|反向代理| C[后端服务器组]
                        C --> D[Tomcat]
                        C --> E[Node.js]
                        C --> F[其他应用服务器]
                        B -.->|负载均衡| C
                        B -.->|缓存| G[缓存层]
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style B fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                        style C fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>

        <!-- Section 1: 反向代理配置 -->
        <div class="max-w-4xl mx-auto mb-12">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12 card-hover">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    <i class="fas fa-exchange-alt mr-3 text-purple-600"></i>反向代理配置
                </h2>
                
                <h3 class="text-2xl font-semibold mb-6 text-gray-700">1.1 基本反向代理配置</h3>
                <p class="text-lg leading-relaxed text-gray-600 mb-6 indent-8">
                    Nginx 可以通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">proxy_pass</code> 指令将请求转发到后端应用服务器。最基本的配置示例如下：
                </p>
                
                <div class="code-block mb-6">
                    <pre><code>server {
    listen 80;
    server_name demo.com;

    location / {
        proxy_pass http://backend;
    }
}</code></pre>
                </div>
                
                <p class="text-lg leading-relaxed text-gray-600 indent-8">
                    在这个示例中，Nginx 将所有到达 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">demo.com</code> 的请求转发到 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">http://backend</code>，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">backend</code> 可以是 Tomcat、Node.js 或其他应用服务器。
                </p>
            </div>
        </div>

        <!-- Section 2: 负载均衡 -->
        <div class="max-w-4xl mx-auto mb-12">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12 card-hover">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    <i class="fas fa-balance-scale mr-3 text-purple-600"></i>负载均衡
                </h2>
                
                <h3 class="text-2xl font-semibold mb-6 text-gray-700">2.1 配置负载均衡</h3>
                <p class="text-lg leading-relaxed text-gray-600 mb-6 indent-8">
                    Nginx 支持将请求分发到多个后端服务器，实现负载均衡。这可以通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">upstream</code> 模块来配置：
                </p>
                
                <div class="code-block mb-6">
                    <pre><code>http {
    upstream backend {
        server backend1.demo.com;
        server backend2.demo.com;
    }

    server {
        listen 80;
        server_name demo.com;

        location / {
            proxy_pass http://backend;
        }
    }
}</code></pre>
                </div>
                
                <p class="text-lg leading-relaxed text-gray-600 indent-8">
                    在这个配置中，Nginx 会根据负载均衡算法（默认是轮询）将请求分发到 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">backend1</code> 和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">backend2</code>。
                </p>
            </div>
        </div>

        <!-- Section 3: 配置代理请求头 -->
        <div class="max-w-4xl mx-auto mb-12">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12 card-hover">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    <i class="fas fa-cog mr-3 text-purple-600"></i>配置代理请求头
                </h2>
                
                <h3 class="text-2xl font-semibold mb-6 text-gray-700">3.1 设置请求头</h3>
                <p class="text-lg leading-relaxed text-gray-600 mb-6 indent-8">
                    Nginx 可以设置一些 HTTP 头部信息，如 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Host</code>、<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">X-Real-IP</code> 和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">X-Forwarded-For</code>，这些信息通常被应用服务器用来处理请求：
                </p>
                
                <div class="code-block mb-8">
                    <pre><code>location / {
    proxy_pass http://backend;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}</code></pre>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div class="bg-purple-50 rounded-lg p-4">
                        <p class="list-item text-gray-700">
                            <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">Host</code>：原始请求的主机名。
                        </p>
                    </div>
                    <div class="bg-purple-50 rounded-lg p-4">
                        <p class="list-item text-gray-700">
                            <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">X-Real-IP</code>：客户端的真实 IP 地址。
                        </p>
                    </div>
                    <div class="bg-purple-50 rounded-lg p-4">
                        <p class="list-item text-gray-700">
                            <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">X-Forwarded-For</code>：客户端 IP 地址链，便于追踪请求来源。
                        </p>
                    </div>
                    <div class="bg-purple-50 rounded-lg p-4">
                        <p class="list-item text-gray-700">
                            <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">X-Forwarded-Proto</code>：请求使用的协议（HTTP 或 